package com.zhangst.bbs.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhangst.bbs.dao.BaseDaoI;
import com.zhangst.bbs.domain.Cate;
import com.zhangst.bbs.domain.Content;
import com.zhangst.bbs.page.DataList;
import com.zhangst.bbs.service.ContentServiceI;

@Service("contentService")
public class ContentServiceImpl implements ContentServiceI{
	BaseDaoI<Content> contentDao;
	
	
	
	public BaseDaoI<Content> getContentDao() {
		return contentDao;
	}
	
	@Autowired
	public void setContentDao(BaseDaoI<Content> contentDao) {
		this.contentDao = contentDao;
	}

	@Override
	public void add(Content content) {
		contentDao.save(content);
	}

	@Override
	public void edit(Content content) {
		contentDao.update(content);
		
	}

	@Override
	public void del(Content content) {
		contentDao.delete(content);
		
	}

	@Override
	public DataList getContents(int cate_id, int page, int rows) {
		Cate cate = new Cate();
		cate.setId(cate_id);
		DataList list = new DataList();
		list.setPage(page);
		list.setNum(rows);
		String hql="from Content content where content.cate =:cate";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("cate", cate);
		list.setRows(contentDao.find(hql, params, page, rows));
		Long total = contentDao.count("select count(*) from Content content where content.cate =:cate",params);
		list.setTotal(total);;
		return list;
	}

	@Override
	public List<Content> getContents(int user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DataList getContents(String keyword, int page, int rows) {
		DataList list = new DataList();
		list.setPage(page);
		String hql ="from Content content where content.title like '%"+keyword+"%'";
		Long total = contentDao.count("select count(*) from Content content where content.title like '%"+keyword+"%'");
		list.setRows(contentDao.find(hql, page, rows));
		list.setTotal(total);
		return list;
	}

	@Override
	public Content getContent(int id) {
		String hql="from Content content where content.id=:id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return contentDao.get(hql, params);
	}

}
